package com.spdg.ring;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import cn.wolf.base.BaseActivity;
import cn.wolf.http.HttpCallBack;
import cn.wolf.tools.AlertHelper;
import cn.wolf.tools.Base64Util;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.bo.CheckVersionBo;
import com.spdg.ring.entity.VersionEntity;
import com.spdg.ring.entity.VersionEntity.Content;
import com.spdg.ring.resp.VersionResp;
import com.spdg.ring.util.GlobalExceptionHanlder;
import com.umeng.analytics.MobclickAgent;

/**
 * <p>Title:  福州绕圈圈activity基类</p>
 * <p>Description: 所有activity需继承此类 </p>
 * <p>@author: caijj                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-6             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public abstract class RingBaseActivity extends BaseActivity {

	private View mLoadingTip; // 进度栏
	private View mReturn; // 返回
	private String versionUpdateUrl = ""; // 版本升级下载地址
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mReturn = findViewById(R.id.btn_return);
		if(mReturn != null){
			mReturn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					onBackPressed();
				}
			});
		}
		
		GlobalExceptionHanlder.getInstance().register(mContext);
	}

	@Override
	protected void setCustomTitleTop() {
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(mContext); // 友盟统计数据

		onEvent(); // 统计访问该activity次数
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(mContext); // 友盟统计数据
	}
	
	public void showProgressBar() {
		// progress.show();
		if (mLoadingTip == null) {
			mLoadingTip = findViewById(R.id.loading_bar);
		}
		if (mLoadingTip != null) {
			mLoadingTip.setVisibility(View.VISIBLE);
		}
	}

	public void hideProgressBar() {
		// progress.hide();
		if (mLoadingTip == null) {
			mLoadingTip = findViewById(R.id.loading_bar);
		}
		if (mLoadingTip != null) {
			mLoadingTip.setVisibility(View.GONE);
		}
	}

	/**
	 * 统计activity访问次数
	 */
	protected void onEvent() {
		String eventID = getEventID();
		if (!StringUtil.isEmpty(eventID))
			MobclickAgent.onEvent(mContext, eventID);
	}

	protected abstract String getEventID();

	/**
	 * 系统菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.system_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if(id == R.id.menu_about){
			Intent about = new Intent();
			about.setClass(mContext, AboutActivity.class);
			startActivity(about);
		} else if (id == R.id.menu_checkversion){
			CheckVersionBo mCheckVerBo = new CheckVersionBo(mContext, new CheckVerCallBack());
			mCheckVerBo.checkVersion();
		} else if (id == R.id.menu_exit){
			AlertHelper.showConfirm(mContext, getString(R.string.exit_dialog_title),
					getString(R.string.exit_dialog_message), getString(R.string.exit_dialog_yes),
					getString(R.string.exit_dialog_no), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							exitApp();
						}
					}, null);
		}
		return true;
	}
	
	class CheckVerCallBack implements HttpCallBack<VersionResp> {

		@Override
		public void call(VersionResp response) {
			if (response.isSuccess()) {

				VersionEntity entity = response.getEntity();
				Content content = entity.getContent();
				String update = Base64Util.decode2String(content.getUpdate());
				versionUpdateUrl = Base64Util.decode2String(content.getUrl());

				if (CheckVersionBo.VERSION_UPDATE.equals(update)) {
					String tip = getString(R.string.check_version_new);
					AlertHelper.showConfirm(mContext, tip, new OnConfirmClickListener(), null);
				}
			}

		}

		@Override
		public void progress(Object... obj) {
		}

		@Override
		public void onNetWorkError() {
			Toast.makeText(mContext, getString(R.string.network_error), Toast.LENGTH_SHORT).show();
		}
	}

	// 升级版本
	class OnConfirmClickListener implements android.content.DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			// 调用本地浏览器进行版本下载
			Uri uri = Uri.parse(versionUpdateUrl);
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(intent);
		}
	}
	
	
}
